// import router from '@/credit/router'
import { getData } from '@vimi/utils-api'
// import { dateFormat } from '@/common/utils/common'
// import { POSTLOAN_WARNING_DAY } from '@/credit/plugins/constant'
import { service } from '@vimi/utils-api'
const getFirstData = res => {
  return (res && res.data && res.data.length && res.data[0]) || {}
}

const state = {
  creditData: {
    view: {}, // customer_related_view
    info: {}, // datum_customer_info
  },
  warningCount: 0,
  taskCount: {},
  taskCountMission: {},
  taskCountTown: {},
  taskCountGxs: {},
  taskCountQyd: {},
  taskCountHnd: {},
  taskCountFast: {},
  taskCountTownProvince: {},
  taskCountProvinceUnion: {},
  taskResCountFast: {},
  taskResCountFastPostloan: {},
  taskCountBusUnion: {},
  taskCountBusSelf: {},
}

const getters = {
  creditData: state => state.creditData,
  warningCount: state => state.warningCount,
  taskCount: state => state.taskCount,
  taskCountMission: state => state.taskCountMission,
  taskCountTown: state => state.taskCountTown,
  taskCountGxs: state => state.taskCountGxs,
  taskCountQyd: state => state.taskCountQyd,
  taskCountHnd: state => state.taskCountHnd,
  taskCountTownProvince: state => state.taskCountTownProvince,
  taskCountProvinceUnion: state => state.taskCountProvinceUnion,
  taskCountFast: state => state.taskCountFast,
  taskResCountFast: state => state.taskResCountFast,
  taskResCountFastPostloan: state => state.taskResCountFastPostloan,
  taskCountBusUnion: state => state.taskCountBusUnion,
  taskCountBusSelf: state => state.taskCountBusSelf,
}

// this.$store.dispatch('...')
const actions = {
  async getWarningCount({ commit, rootGetters }, force = false) {
    if (!force && state.warningCount.user_id) {
      return Promise.resolve(state.warningCount)
    }
    const res = await getData('credit', 'create_related_lod_beh', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)
    commit('warningCount', data)
    return data
  },

  async getTaskCount({ commit, rootGetters }) {
    const tableName = 'credit_my_task_list_view'
    const res = await getData('credit', tableName, {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCount', data)
    return data
  },

  async getTaskCountMission({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_city_view', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountMission', data)
    return data
  },

  async getTaskCountTown({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_qx_view', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountTown', data)
    return data
  },

  async getTaskCountTownProvince({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_qx_gua4_view', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountTownProvince', data)
    return data
  },

  async getTaskCountGxs({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_gxs_view', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountGxs', data)
    return data
  },

  async getTaskCountQyd({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_qy_view', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountQyd', data)
    return data
  },

  async getTaskCountHnd({ commit, rootGetters }) {
    const res = await getData('credit', 'datum_one_county_industry_view_db', {
      isBIQuery: 1,
      'service_id.eq': rootGetters.clientId,
    })
    const data = (res && res.data) || []
    commit('taskCountHnd', data)
    return data
  },

  async getTaskCountProvinceUnion({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_pro_gua', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountProvinceUnion', data)
    return data
  },

  async getTaskCountFast({ commit, rootGetters }) {
    const res = await getData('credit', 'credit_my_task_list_qx_gua4_quick_view', {
      'user_id.eq': rootGetters.userId,
    })
    const data = getFirstData(res)

    commit('taskCountFast', data)
    return data
  },

  async getTaskResCountFast({ commit, rootGetters }) {
    const res = await getData('credit', 'to_do_list_28_view_db_quick', {
      'orWhere.res_a.eq': rootGetters.userId,
      'orWhere.res_b.eq': rootGetters.userId,
      groupFunc: `round(sum( todo_claim_total)),round(sum(todo_item_total)),round(sum(todo_approval_total))`,
    })
    const res2 = await getData('credit', 'task_list_view_db_quick', {
      index: 1,
      size: 1,
      'confuse_status.neq': 3,
      'task_status.eq': 0,
      'task_person.eq': rootGetters.userId,
    })
    const invest = res2?.data?.totalCount ?? 0
    const data = getFirstData(res)
    data.invest = invest
    commit('taskResCountFast', data)
    return data
  },
  async getTaskResCountFastPostloan({ commit, rootGetters }) {
    const res = await getData('credit', 'to_do_list_28_view_beh_db_quick', {
      'orWhere.res_a.eq': rootGetters.userId,
      'orWhere.res_b.eq': rootGetters.userId,
      groupFunc: `round(sum( todo_exp_total))`,
    })
    const data = getFirstData(res)
    commit('taskResCountFastPostloan', data)
    return data
  },
  async getTaskCountBusUnion({ commit, rootGetters }) {
    const res = await getData('credit', 'task_list/statistics/', {
      'apiKey.kv': 'todo-task',
      'task_person.kv': rootGetters.userId,
    })

    const data = {
      p: res.data?.todo_claim_total ?? 0,
      p1: res.data?.todo_invest_total ?? 0,
      p2: res.data?.todo_contract_handle_total ?? 0,
      p3: res.data?.todo_sign_total ?? 0,
      p4: res.data?.todo_loan_apply_total ?? 0,
      p5: res.data?.todo_loan_register_total ?? 0,
      p6: res.data?.todo_exp_total ?? 0,
    }
    commit('taskCountBusUnion', data)
    return data
  },
  async getTaskCountBusSelf({ commit, rootGetters }) {
    const res = await service.get(
      `/api/credit/task_list/statistics/?apiKey=todo-task-dir&task_person=${rootGetters.userId}`
    )
    const data = res.data || {}
    commit('taskCountBusSelf', data)
    return data
  },
}

const mutations = {
  creditData: (state, payload) => (state.creditData = payload),
  warningCount: (state, payload) => (state.warningCount = payload),
  taskCount: (state, payload) => (state.taskCount = payload),
  taskCountMission: (state, payload) => (state.taskCountMission = payload),
  taskCountTown: (state, payload) => (state.taskCountTown = payload),
  taskCountGxs: (state, payload) => (state.taskCountGxs = payload),
  taskCountQyd: (state, payload) => (state.taskCountQyd = payload),
  taskCountHnd: (state, payload) => (state.taskCountHnd = payload),
  taskCountTownProvince: (state, payload) => (state.taskCountTownProvince = payload),
  taskCountProvinceUnion: (state, payload) => (state.taskCountProvinceUnion = payload),
  taskCountFast: (state, payload) => (state.taskCountFast = payload),
  taskResCountFast: (state, payload) => (state.taskResCountFast = payload),
  taskResCountFastPostloan: (state, payload) => (state.taskResCountFastPostloan = payload),
  taskCountBusUnion: (state, payload) => (state.taskCountBusUnion = payload),
  taskCountBusSelf: (state, payload) => (state.taskCountBusSelf = payload),
}

export default {
  state,
  getters,
  actions,
  mutations,
}
